// pages/agent/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pickercus: ['意向客户', '定向客户'],
    index: 0,
    index2: null,
    isTel: '',
    isName: '',
    isHouse: '',
    date: '2019-12-19',
    picker: [],
    proId: [],
    searchvalue: '',
    remarks:''
  },
  // 姓名
  nameVal(e) {
    this.setData({
      isName: e.detail.value
    })
  },
  // 电话
  telVal(e) {
    this.setData({
      isTel: e.detail.value
    })
  },
  // 楼盘
  searchlist(e) {
    this.setData({
      searchvalue: e.detail.value
    })
  },

  /**
   * 备注信息
   */
  bindRemarks(e){
    this.setData({
      remarks: e.detail.value
    })
  },
  //搜索框内容
  searchdesc() {
    var that = this;
    var setid = app.globalData.cityCode;
    var searchvalue = that.data.searchvalue;
    if (searchvalue == '') {
      wx.showToast({
        title: '请输入内容',
        duration: 1000,
        icon: 'none'
      })
    } else {
      wx.showLoading({
        title: '加载中',
      })
      app.core('get', 'getNewHouseProjectBySiteId?siteId=' + setid + '&projectName=' + searchvalue, '', (res) => {
        console.log(res.data)
        let proName = []
        let proId = []
        for (let i in res.data) {
          proName.push(res.data[i].projectName)
          proId.push(res.data[i].id)
        }
        console.log(proName)
        console.log(proId)
        this.setData({
          picker: proName,
          proId
        })
        if (res.data == '') {
          wx.showToast({
            title: '暂无相关信息,请输入正确楼盘，支持模糊搜索',
            duration: 2000,
            icon: 'none'
          })
          return false
        }
        wx.hideLoading()
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  PickerChangeCustomer(e) {
    console.log(e);
    this.setData({
      index2: e.detail.value
    })
    let index2 = this.data.index2
    console.log(index2)
  },
  // PickerChange(e) {
  //   console.log(e);
  //   this.setData({
  //     index: e.detail.value
  //   })
  //   let index = this.data.index
  //   console.log(this.data.proId[index])
  // },

  selectitem(e) {
    console.log(e.detail.value)
    let index = e.detail.value
    this.setData({
      index: index
    })
    let proId = this.data.proId
    console.log(proId[index])
  },
  // DateChange(e) {
  //   console.log(e)
  //   this.setData({
  //     date: e.detail.value
  //   })
  // },
  // 报备post
  Registerpost() {
    var openid = wx.getStorageSync('user_new');
    var vm = this;
    var isTel = vm.data.isTel;
    var isName = vm.data.isName;
    var remarks = vm.data.remarks;
    let index2 = vm.data.index2
    let index = vm.data.index
    let project_id = vm.data.proId[index]
    if (vm.data.isName == '' || isTel == '') {
      wx.showToast({
        title: '带 * 为必填',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    else if (!(/^[\u4E00-\u9FA5]{2,4}$/.test(isName))) {
      wx.showToast({
        title: '姓名格式错误！请重新输入',
        icon: 'none',
        duration: 2000
      });
      return false;
    } else if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(isTel))) {
      wx.showToast({
        title: '手机号码格式错误！请重新输入',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    else if (index2 == null) {
      wx.showToast({
        title: '请选择客户类型',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    else {
      let user_name = vm.data.isName
      let user_phone = vm.data.isTel
      let index = vm.data.index
      let project_id = vm.data.proId[index]
      let index2 = vm.data.index2
      // let project_id = proId[index]
      console.log(index, project_id)
      // console.log(index2)
      let open_id = wx.getStorageSync('user_new')
      // console.log(user_name, user_phone, project_id, open_id);
      app.core('get', 'adviserReport', {
        'user_name': user_name,
        'user_phone': user_phone,
        'open_id': open_id,
        'project_id': project_id,
        'status': index2,
        'remarks': remarks
      }, (res) => {
        console.log(res)
        if (res.resultCode == 200) {
          wx.showToast({
            title: '报备成功',
            icon: 'success',
            duration: 2000
          });
          setTimeout(function () {
            wx.switchTab({
              url: '/pages/member/index',
            });
          }, 1000)
        } else {
          wx.showToast({
            title: '网络出错！',
            icon: 'none',
            duration: 2000
          });
        }
      },
        (err) => {
          console.log(err)
        }
      )

    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})